const Controller = require("egg").Controller;
const { success, fail } = require("../utils/code");
const { list_to_tree, get_all_folder_id } = require("../utils/tools");
const fs = require("fs");
const Md5 = require("md5");
const path = require('path');

const handle_update_file = (list) => {
  list.map((item) => {
    fs.writeFileSync(`app/public/${item.filename}`, item.file);
    const { file, ...rest } = item;
  });
};

class FileController extends Controller {
  async get_video() {
    const { ctx } = this;
    // ctx.body = ctx.helper.getFileStream('/public/videos/demo.mp4');

    const filePath = path.join(ctx.app.config.baseDir, 'app/public', 'videos', 'demo.mp4');
    ctx.set('Content-Type', 'video/mp4');
    ctx.body = fs.createReadStream(filePath);
  }
  async folder_list() {
    const { ctx } = this;
    const result = await ctx.service.file.folder_list();
    if (result) {
      ctx.body = success(list_to_tree(result));
    } else {
      ctx.body = fail();
    }
  }

  async file_list() {
    const { ctx } = this;
    const { folder_id = "" } = ctx.request.query;
    const folder_data = await ctx.service.file.folder_list();
    let all_folder_id = get_all_folder_id(folder_data, folder_id)
      .map((item) => item.id)
      .join();
    all_folder_id = `${all_folder_id},${folder_id}`;
    const result = await ctx.service.file.file_list(all_folder_id);

    if (result) {
      ctx.body = success(result);
    } else {
      ctx.body = fail();
    }
  }
  async uplode_file() {
    const { ctx } = this;
    const { folder_id } = ctx.request.body;
    let update_files = [];
    let update_data = [];
    this.ctx.request.files.forEach(async (item) => {
      let file = fs.readFileSync(item.filepath);
      const md5 = Md5(file);

      const { file_md5 = "", name = "" } =
        (await this.ctx.service.file.get_name_by_md5(md5)) || {};

      if (file_md5 === md5 && item.filename === name) return;
      if (file_md5 === md5 && item.filename !== name) {
        update_data.push({
          folder_id,
          name: item.filename,
          path: `app/public/${item.filename}`,
          file_md5,
        });
      }
      const { file_md5: current_md5 = "" } =
        (await this.ctx.service.file.get_md5_by_name(item.filename)) || {};
      if (current_md5 !== md5) {
        update_files.push({
          folder_id,
          name: item.filename,
          path: `app/public/${item.filename}`,
          file_md5: md5,
          file,
        });
      }
    });

    handle_update_file(update_files);
    await ctx.service.file.handle_update_file(update_files);
    // await ctx.service.file.handle_update_data(update_data);

    // const result = await Promise.all([
    //   await ctx.service.file.handle_update_file(update_files),
    //   await ctx.service.file.handle_update_data(update_data),
    // ]);

    // if (result) {
    //   ctx.body = success();
    // } else {
    //   ctx.body = fail();
    // }
  }
}
module.exports = FileController;
